package com.lb.jwitter.service;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.*;
import com.lb.jwitter.hibernate.*;
import com.lb.jwitter.util.ChumanoLogger;
import com.lb.jwitter.util.ConditionReport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

@Service("tbl_CTDT_MonHocService")
@Transactional
@WebService(endpointInterface = "com.lb.jwitter.service.Tbl_CTDT_MonHocService")
public class Tbl_CTDT_MonHocServiceImpl implements Tbl_CTDT_MonHocService,
		Serializable {

	private Log log = LogFactory.getLog(Tbl_CTDT_MonHocServiceImpl.class);
	private static final long serialVersionUID = -8740095022264495229L;

	@Autowired
	@Qualifier("tbl_CTDT_MonHocDAO")
	private Tbl_CTDT_MonHocDAO _tbl_CTDT_MonHocDao;

	public final List<Tbl_CTDT_MonHoc> findAlltbl_CTDT_MonHoc(int pageSize,
			int page) {
		List<Tbl_CTDT_MonHoc> result = get_tbl_CTDT_MonHocDao().findAll(
				pageSize, page);
		log.info("Number of user in DB:: " + result.size());
		return result;
	}

	@Transactional(noRollbackFor = IllegalArgumentException.class)
	public final Long createtbl_CTDT_MonHoc(Tbl_CTDT_MonHoc obj) {
		return _tbl_CTDT_MonHocDao.save(obj);
	}

	public final void updatetbl_CTDT_MonHoc(Tbl_CTDT_MonHoc obj) {
		_tbl_CTDT_MonHocDao.update(obj);
	}// end update methode

	public final Tbl_CTDT_MonHoc gettbl_CTDT_MonHoc(Long id) {
		log.info("Try to retrieve the tbl_CTDT_MonHoc: " + id);
		return get_tbl_CTDT_MonHocDao().get(id);
	}

	public void deletetbl_CTDT_MonHoc(Long id) {
		log.info("Try to delete the tbl_CTDT_MonHoc by Id : " + id);
		if (_tbl_CTDT_MonHocDao.exists(id)) {
			_tbl_CTDT_MonHocDao.delete(id);
		} else {
			throw new IllegalArgumentException("EmployeeId '" + id
					+ "' does not exists");
		}
	}

	public void deleteListtbl_CTDT_MonHoc(String[] listId) {
		for (String id : listId) {
			deletetbl_CTDT_MonHoc(Long.parseLong(id));
		}
	}

	public List<Tbl_CTDT_MonHoc> findtbl_CTDT_MonHocByCondition(
			List<ConditionReport> cons, int pageSize, int page) {
		return _tbl_CTDT_MonHocDao.findByCondition(Tbl_CTDT_MonHoc.class, cons,
				pageSize, page);
	}

	public int counttbl_CTDT_MonHocByCondition(List<ConditionReport> cons) {
		return _tbl_CTDT_MonHocDao
				.countByCondition(Tbl_CTDT_MonHoc.class, cons);
	}

	public final Tbl_CTDT_MonHocDAO get_tbl_CTDT_MonHocDao() {
		return _tbl_CTDT_MonHocDao;
	}

	public final void set_tbl_CTDT_MonHocDao(Tbl_CTDT_MonHocDAO obj) {
		this._tbl_CTDT_MonHocDao = obj;
	}

	public void createListtbl_CTDT_MonHocByJSON(String data) {
		try {
		ChumanoLogger.init(Tbl_CTDT_MonHocServiceImpl.class.getName());
		ChumanoLogger.log("0BAT DAU INSERT DU LIEU");
		List<Tbl_CTDT_MonHoc> tbl_CTDT_MonHocs = new ArrayList<Tbl_CTDT_MonHoc>();

		JSONArray jsons = (JSONArray) JSONSerializer.toJSON(data);

		for (int i = 0; i < jsons.size(); i++) {
				JSONObject obj = jsons.getJSONObject(i);
				String logStr ="";
				
				// MA TEN LOAI HEDAOTAO SOTINCHI SOTIET TRANGTHAI
				// -----------------Lay du lieu--------------------------
				// ma
				String ma = obj.getString("MA");

				// ten
				String ten = obj.getString("TEN");

				// loai
				String loai = obj.getString("LOAI");

				// Hedaotao
				String hedt = obj.getString("HEDAOTAO");

				// SOTINCHI
				String sotinchi = obj.getString("SOTINCHI");

				// SOTIET
				String sotiet = obj.getString("SOTIET");

				// TRANGTHAI
				// String tt = obj.getString("TRANGTHAI");
				// int trangthai = (tt.equals("x"))? 0: 1;
				
				logStr ="[ " + (i+1)+ " - " 
				+ "MA: " + ma + " - " 
				+ "TEN: " + ten + " - " 
				+ "LOAI: " + loai + " - " 
				+ "SOTINCHI: " + sotinchi + " - " 
				+ "SOTIET: " + sotiet + " - " 
				+ "HDT: " + hedt 
				+ "]";
				
				// ======================================================
				// tim mon hoc
				Tbl_CTDT_MonHoc tbl_CTDT_MonHoc = new Tbl_CTDT_MonHoc();
				
				List<Long> monHocId = findMonHocId(ma);
				if (monHocId != null && monHocId.size() > 0) {
					tbl_CTDT_MonHoc.setId(monHocId.get(0));
					logStr += " YES MAMH";
				}
				
				tbl_CTDT_MonHoc.setMa(ma);
				tbl_CTDT_MonHoc.setTen(ten);
				tbl_CTDT_MonHoc.setLoai(loai);
				tbl_CTDT_MonHoc.setSoTiet(Integer.parseInt(sotiet));
				tbl_CTDT_MonHoc.setSoTinChi(Double.parseDouble(sotinchi));
				tbl_CTDT_MonHoc.setHeDaoTao(hedt);
				//tbl_CTDT_MonHoc.setTrangThai(1);
				tbl_CTDT_MonHocs.add(tbl_CTDT_MonHoc);
				
				ChumanoLogger.log(logStr);
			
		}// end for
		_tbl_CTDT_MonHocDao.saveOrUpdateAll(tbl_CTDT_MonHocs);
		} catch (Exception e) {
			ChumanoLogger.log("ERROR: " +e.getMessage());
			System.out.println("------------ERROR CHUMANO-------------");
			e.printStackTrace();
			System.out.println("=================================");
		}
		ChumanoLogger.stop();
	}

	private List<Long> findMonHocId(String maMonHoc) {
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("ma");
			con.setValue1(maMonHoc);
			con.setOperator("eq");
			cons.add(con);
		}
		List<String> columns = new ArrayList<String>();
		columns.add("id");

		List<Long> monHocId = _tbl_CTDT_MonHocDao.findColumnByCondition(Tbl_CTDT_MonHoc.class, cons, columns, 1, 1);
		return monHocId;
	}
}// endclass

